﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HtmlMultiFileEditor.Model;

namespace HtmlMultiFileEditor.Controller
{
    public class FileHandlerController
    {

        #region "Singleton"

        private static FileHandlerController fileHandlerController = new FileHandlerController();
        private FileHandlerController() { }
        public static FileHandlerController Instance
        {
            get { return fileHandlerController; }
        }

        #endregion

        #region "Events"

        public delegate void FileListChangedEventHandler(object sender, string[] eFileList);
        public event FileListChangedEventHandler FileListChanged;

        public delegate void CurrentFileChangeEventHandler(object sender, string eCurrentFile);
        public event CurrentFileChangeEventHandler currentFileChanged;

        #endregion

        #region "Private"

        private FileHandler fileHandler = FileHandler.Instance;

        private void SetCurrentFile(string currentFile)
        {
            fileHandler.CurrentFile = currentFile;
            if (currentFileChanged != null)
                currentFileChanged(this, fileHandler.CurrentFile);
        }

        #endregion

        #region "Public"

        public void SetFilePath(string filePath)
        {

            fileHandler.FilePath = filePath;

        }

        public void SetFileListSelected(string[] fileListSelected)
        {
            fileHandler.FileListSelected = fileListSelected;
            if (fileHandler.FileListSelected.Length > 0)
                SetCurrentFile(fileHandler.FileListSelected[0]);
        }

        public void SetFileList(string filePath)
        {
            if (filePath.EndsWith(".htm") || filePath.EndsWith(".html"))
            {
                string[] fileList = { filePath };
                fileHandler.FileList = fileList;
            }
            else
                fileHandler.FileList = fileHandler.FindFileList(filePath);

            if (FileListChanged != null)
                FileListChanged(this, fileHandler.FileList);


            if (fileHandler.FileList.Length > 0)
                SetCurrentFile(fileHandler.FileList[0]);
        }

        public void SetFileList(string[] fileList)
        {

            fileHandler.FileList = fileList;
            if (FileListChanged != null)
                FileListChanged(this, fileHandler.FileList);

            if (FileListChanged != null)
                FileListChanged(this, fileHandler.FileList);

            if (fileHandler.FileList.Length > 0)
                SetCurrentFile(fileHandler.FileList[0]);
        }



        public void NextFile()
        {

            if (fileHandler.NextFile() && currentFileChanged != null)
            {
                currentFileChanged(this, fileHandler.CurrentFile);
            }
        }

        public void PreviousFile()
        {
            if (fileHandler.PreviousFile() && currentFileChanged != null)
            {
                currentFileChanged(this, fileHandler.CurrentFile);
            }

        }

        #endregion







    }
}
